package com.uow.algdemo.ui;

import java.io.FileOutputStream;

import com.uow.algdemo.ui.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class SettingActivity extends Activity {

    @Override
    /**
     * Main method of activity.
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        String[] options = new String[1];
        options[0] = "Clear history records";
        final ListView list = (ListView) findViewById(R.id.listOptions);
        ArrayAdapter<Object> arrayAdapter = new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, options);
        list.setAdapter(arrayAdapter);
        list.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        	{
            	if(arg2 == 1)
            	{
	                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
	                builder.setTitle("Are you sure to clean all history records?");
	                builder.setIcon(android.R.drawable.ic_dialog_info);
	                builder.setNegativeButton("Cancel", null);
	                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
	                {
	                    public void onClick(DialogInterface dialog, int which)
	                    {
		            		String empty = "";
							try
							{
								byte [] bytes = empty.getBytes();
								FileOutputStream fout = openFileOutput("sort.txt", MODE_PRIVATE);
								fout.write(bytes);
								fout.close();
								fout = openFileOutput("string.txt", MODE_PRIVATE);
								fout.write(bytes);
								fout.close();
								fout = openFileOutput("graph.txt", MODE_PRIVATE);
								fout.write(bytes);
								fout.close();
							}
							catch(Exception e)
							{
								e.printStackTrace();
							}
	                    }
	                });
	                builder.show();
            	}
        	}
        });
    }
}
